//
//  JHBaseViewController.m
//  JuniRengLish_iOS
//
//  Created by revogi on 2019/5/8.
//  Copyright © 2019 revogi. All rights reserved.
//

#import "JHBaseViewController.h"
#import "MBProgressHUD.h"

@interface JHBaseViewController ()
@property (nonatomic, strong) MBProgressHUD *loadingHUD;
@property (nonatomic, strong) MBProgressHUD *progressHUD;
@end

@implementation JHBaseViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor=[UIColor whiteColor];
    [self initUI];
}

- (void)initUI
{
    
    
}

#pragma mark - show loading ..

- (void)showLoadingDialog:(BOOL)animated {
    [self showLoadingDialog:@"" animated:animated];
}

- (void)showLoadingDialogWithTitle:(NSString*)title {
    [self showLoadingDialog:title animated:NO];
}

- (void)showLoadingDialog:(NSString*)title animated:(BOOL)animated {
    [self showLoadingDialog:title message:nil animated:animated];
}

- (void)showLoadingDialog:(NSString*)title message:(NSString*)message {
    [self showLoadingDialog:title message:message animated:NO];
}

- (void)showLoadingDialog:(NSString*)title message:(NSString*)message animated:(BOOL)animated {
    if (self.loadingHUD != nil) {
        [self.loadingHUD hideAnimated:NO];
        self.loadingHUD = nil;
    }
    
    self.loadingHUD = [MBProgressHUD showHUDAddedTo:self.navigationController ? self.navigationController.view : self.view animated:animated];
    self.loadingHUD.mode = MBProgressHUDModeIndeterminate;
    self.loadingHUD.bezelView.color = JKColorAlpha(0.8f, 0.8f, 0.8f, 0.8f);
    
    self.loadingHUD.label.text = title;
    self.loadingHUD.label.textColor = JKColor(126, 126, 126);
    if (message.length > 0) {
        self.loadingHUD.detailsLabel.text = message;
    }
}

- (void)dismissLoadingDialog:(BOOL)animated {
    [self.loadingHUD hideAnimated:animated];
}

- (void)showProgressDialog:(NSString*)title message:(NSString*)message progress:(float)progress {
    if (self.progressHUD == nil) {
        self.progressHUD = [MBProgressHUD showHUDAddedTo:self.navigationController ? self.navigationController.view : self.view animated:YES];
        self.progressHUD.mode = MBProgressHUDModeAnnularDeterminate;
        self.progressHUD.bezelView.color = JKColorAlpha(0.8f, 0.2f, 0.2f, 0.2f);
    }
    
    self.progressHUD.label.text = title;
    if (message.length > 0) {
        self.progressHUD.detailsLabel.text = message;
    }
    self.progressHUD.progress = progress;
}

- (void)dismissProgressDialog:(BOOL)animated {
    [self.progressHUD hideAnimated:animated];
}

- (void)dismissAllHUD:(BOOL)animated {
    [MBProgressHUD hideHUDForView:self.navigationController ? self.navigationController.view : self.view animated:animated];
}

- (void)showAlertOK:(NSString*)title message:(NSString*)message {
    [self showAlert:title message:message buttons:@[[AlertButton initWithText:@"确定" handler:nil]]];
}

- (void)showAlertOK:(NSString*)title message:(NSString*)message handler:(AlertButtonHandler)handler {
    [self showAlert:title message:message buttons:@[[AlertButton initWithText:@"确定" handler:handler]]];
}

- (void)showAlertOK:(NSString*)title message:(NSString*)message buttonText:(NSString*)button handler:(AlertButtonHandler)handler {
    [self showAlert:title message:message buttons:@[[AlertButton initWithText:button handler:handler]]];
}

-(void)showAlert:(NSString *)title
message:(NSString *)message
buttonHandler:(AlertButtonHandler)handler{
    AlertButton *ok = [AlertButton initWithText:@"确定" style:UIAlertActionStyleDefault handler:handler];
    AlertButton *cancel = [AlertButton initWithText:@"取消" style:UIAlertActionStyleCancel handler:nil];
    [self showAlert:title message:message buttons:@[cancel, ok]];
}

- (void)showAlert:(NSString*)title
message:(NSString*)message
buttonText:(NSString*)buttonText
buttonHandler:(AlertButtonHandler)handler
cancelText:(NSString*)cancelText
cancelHandler:(AlertButtonHandler)cancelHandler {
    
    UIAlertController *vc = [JHBaseViewController alertViewControllerWithTitle:title message:message buttonText:buttonText buttonHandler:handler cancelText:cancelText cancelHandler:cancelHandler];
    [self presentViewController:vc animated:YES completion:nil];
}

- (void)showAlert:(NSString*)title
inputField:(AlertControllerHandler)inputHandler
buttonText:(NSString*)buttonText
buttonHandler:(AlertButtonHandler)handler
cancelText:(NSString*)cancelText
cancelHandler:(AlertButtonHandler)cancelHandler {
    
    UIAlertController *vc = [JHBaseViewController alertViewControllerWithTitle:title message:nil buttonText:buttonText buttonHandler:handler cancelText:cancelText cancelHandler:cancelHandler];
    inputHandler(vc);
    [self presentViewController:vc animated:YES completion:nil];
}

- (void)showAlert:(NSString *)title message:(NSString *)message buttons:(NSArray *)buttons {
    UIAlertController *alert = [JHBaseViewController alertViewControllerWithTitle:title  message:message buttons:buttons];
    NSMutableAttributedString *alertControllerMessageStr = [[NSMutableAttributedString alloc] initWithString:message];
    [alertControllerMessageStr addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:14] range:NSMakeRange(0, message.length)];
    [alert setValue:alertControllerMessageStr forKey:@"attributedMessage"];
    [self presentViewController:alert animated:YES completion:nil];
}

- (void)showSheet:(NSString *)title message:(NSString *)message buttons:(NSArray*)buttons {
    UIAlertController *alert = [JHBaseViewController sheetViewControllerWithTitle:title message:message buttons:buttons];
    [self presentViewController:alert animated:YES completion:nil];
}

+ (UIAlertController*)alertViewControllerWithTitle:(NSString*)title
message:(NSString*)message
buttonText:(NSString*)buttonText
buttonHandler:(AlertButtonHandler)handler
cancelText:(NSString*)cancelText
cancelHandler:(AlertButtonHandler)cancelHandler {
    
    AlertButton *ok = [AlertButton initWithText:buttonText style:UIAlertActionStyleDefault handler:handler];
    AlertButton *cancel = [AlertButton initWithText:cancelText style:UIAlertActionStyleDefault handler:cancelHandler];
    return [JHBaseViewController alertViewControllerWithTitle:title message:message buttons:@[ok, cancel]];
}

+ (UIAlertController*)alertViewControllerWithTitle:(NSString*)title message:(NSString*)message buttons:(NSArray*)buttons {
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:title message:message preferredStyle:UIAlertControllerStyleAlert];
    for (AlertButton *button in buttons) {
        NSString *Version = [[UIDevice currentDevice] systemVersion];
        UIAlertAction *action = [UIAlertAction actionWithTitle:button.buttonString style:button.style handler:button.handler];
        if ([Version intValue]>8.3) {
            [action setValue:JKColor(0, 96, 219) forKey:@"_titleTextColor"];
        }
        
        if ([action.title isEqualToString:@"取消"]) {
            if ([Version intValue]>8.3) {
                [action setValue:JKColor(126, 126, 126) forKey:@"_titleTextColor"];
            }
        }
        [alert addAction:action];
    }
    return alert;
}

+ (UIAlertController*)sheetViewControllerWithTitle:(NSString*)title message:(NSString*)message buttons:(NSArray*)buttons {
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:title message:message preferredStyle:UIAlertControllerStyleActionSheet];
    for (AlertButton *button in buttons) {
        UIAlertAction *action = [UIAlertAction actionWithTitle:button.buttonString style:button.style handler:button.handler];
        NSString *Version = [[UIDevice currentDevice] systemVersion];
        if ([Version intValue]>8.3) {
            [action setValue:JKColor(0, 96, 219) forKey:@"_titleTextColor"];
        }
        
        if ([action.title isEqualToString:@"取消"]) {
            if ([Version intValue]>8.3) {
                [action setValue:JKColor(126, 126, 126) forKey:@"_titleTextColor"];
            }
        }
        [alert addAction:action];
        
    }
    return alert;
}
@end



@implementation AlertButton

+ (id)initWithText:(NSString *)title handler:(AlertButtonHandler)handler {
    return [AlertButton initWithText:title style:UIAlertActionStyleDefault handler:handler];
}

+ (id)initWithText:(NSString*)title style:(UIAlertActionStyle)style handler:(AlertButtonHandler)handler {
    AlertButton *button = [[AlertButton alloc] init];
    button.buttonString = title;
    button.style = style;
    button.handler = handler;
    return button;
}

@end


